-- ========================================================================
-- e-commerce database example queries
-- ========================================================================

------------------------------------------------------------------------
-- Basic graph traversals (single hop)
------------------------------------------------------------------------

-- List all products in the "Seafood" category.
SELECT p.name
FROM MATCH (p:product)-[:part_of]->(c:category)
WHERE c.name = 'Seafood';

-- Which brands are located in the city of London?
SELECT b.name
FROM MATCH (b:brand)-[:located_at]->(ci:city)
WHERE ci.name = 'London';

-- What products does the brand "Nike" manufacture?
SELECT p.name
FROM MATCH (b:brand)-[:manufactures]->(p:product)
WHERE b.name = 'Nike';

-- Which customers reside in the city of London?
SELECT cu.company_name, cu.contact_name
FROM MATCH (cu:customer)-[:resides_in]->(ci:city)
WHERE ci.name = 'London';

-- Which customers like the product "Vegie-spread" (the most-liked grocery item)?
SELECT cu.company_name
FROM MATCH (cu:customer)-[:likes]->(p:product)
WHERE p.name = 'Vegie-spread';

------------------------------------------------------------------------
-- Graph analytics (aggregate / business functions)
------------------------------------------------------------------------

-- Top 10 brands by number of followers.
SELECT b.name, COUNT(cu) AS followers
FROM MATCH (cu:customer)-[:follows]->(b:brand)
GROUP BY b.name
ORDER BY followers DESC
LIMIT 10;

-- For each category, how many products do we offer and what is their average unit price?
SELECT c.name, COUNT(p) AS numProducts, AVG(p.unit_price) AS avgUnitPrice
FROM MATCH (p:product)-[:part_of]->(c:category)
GROUP BY c.name
ORDER BY avgUnitPrice DESC;

-- How many products does each supplier carry, ranked? (top 10)
SELECT s.company_name, COUNT(p) AS numProducts
FROM MATCH (p:product)-[:available_at]->(s:supplier)
GROUP BY s.company_name
ORDER BY numProducts DESC
LIMIT 10;

-- Top 10 most-liked products.
SELECT p.name, COUNT(cu) AS likeCount
FROM MATCH (cu:customer)-[:likes]->(p:product)
GROUP BY p.name
ORDER BY likeCount DESC
LIMIT 10;

-- How many products does each brand manufacture? (top 10)
SELECT b.name, COUNT(p) AS numProducts
FROM MATCH (b:brand)-[:manufactures]->(p:product)
GROUP BY b.name
ORDER BY numProducts DESC
LIMIT 10;

------------------------------------------------------------------------
-- Multi-pattern graph queries (joins across relationships)
------------------------------------------------------------------------

-- Find pairs of customers who like the same product.
SELECT c1.company_name AS customer1, c2.company_name AS customer2, p.name AS product
FROM MATCH
  (c1:customer)-[:likes]->(p:product),
  (c2:customer)-[:likes]->(p:product)
WHERE c1.id < c2.id;

-- Find customers and suppliers that are based in the same city.
SELECT cu.company_name AS customer, s.company_name AS supplier, ci.name AS city
FROM MATCH
  (cu:customer)-[:resides_in]->(ci:city),
  (s:supplier)-[:located_in]->(ci:city);

-- Which brands make products that at least one customer likes?
SELECT DISTINCT b.name
FROM MATCH
  (b:brand)-[:manufactures]->(p:product),
  (cu:customer)-[:likes]->(p:product);

-- For customers who follow "Nike", show the products it makes, each product's
-- category, and the suppliers carrying them.
SELECT DISTINCT cu.company_name, p.name AS product, c.name AS category, s.company_name AS supplier
FROM MATCH
  (cu:customer)-[:follows]->(b:brand),
  (b:brand)-[:manufactures]->(p:product),
  (p:product)-[:part_of]->(c:category),
  (p:product)-[:available_at]->(s:supplier)
WHERE b.name = 'Nike';

-- Which suppliers distribute a brand that manufactures products in the "Condiments"
-- category?
SELECT DISTINCT s.company_name, b.name AS brand, p.name AS product
FROM MATCH
  (s:supplier)-[:distributes]->(b:brand),
  (b:brand)-[:manufactures]->(p:product),
  (p:product)-[:part_of]->(c:category)
WHERE c.name = 'Condiments';

------------------------------------------------------------------------
-- Country decoding (graph city vertices + relational countries table)
-- city.country_code is an ISO3 code; join the relational countries table
-- to turn it into a full country name.
------------------------------------------------------------------------

-- List every city with its full country name.
SELECT ci.name AS city, co.name AS country
FROM MATCH (ci:city),
  countries co
WHERE ci.country_code = co.code
ORDER BY country, city;

-- List the customers who reside in France (decoding the country code).
SELECT cu.company_name, ci.name AS city, co.name AS country
FROM MATCH (cu:customer)-[:resides_in]->(ci:city),
  countries co
WHERE ci.country_code = co.code
AND co.name = 'France';

-- How many customers reside in each country?
SELECT co.name AS country, COUNT(cu) AS customers
FROM MATCH (cu:customer)-[:resides_in]->(ci:city),
  countries co
WHERE ci.country_code = co.code
GROUP BY co.name
ORDER BY customers DESC;

-- How many suppliers are located in each country?
SELECT co.name AS country, COUNT(s) AS suppliers
FROM MATCH (s:supplier)-[:located_in]->(ci:city),
  countries co
WHERE ci.country_code = co.code
GROUP BY co.name
ORDER BY suppliers DESC;

-- Which brands are based in the United Kingdom (decoding the country code)?
SELECT b.name AS brand, ci.name AS city, co.name AS country
FROM MATCH (b:brand)-[:located_at]->(ci:city),
  countries co
WHERE ci.country_code = co.code
AND co.name = 'United Kingdom';

------------------------------------------------------------------------
-- Variable-length paths and shortest path (advanced graph)
------------------------------------------------------------------------

-- Find pairs of co-liked products (linked through a common customer) where one of
-- them is manufactured by "Nike".
SELECT DISTINCT p1.name, p2.name
FROM MATCH (p1:product)-[:likes{2,2}]-(p2:product)
WHERE p1.id < p2.id
AND EXISTS (
  SELECT 1
  FROM MATCH (b:brand)-[:manufactures]->(p1:product)
  WHERE b.name = 'Nike'
);

-- Find customers within 2-4 like-hops of customer 5 who also like a product in the
-- "Beverages" category. (Customer 1 likes nothing, so it would return no rows.)
SELECT DISTINCT c2.company_name
FROM MATCH (c1:customer)-[:likes{2,4}]-(c2:customer)
WHERE c1.id = 5
AND c1 <> c2
AND EXISTS (
  SELECT 1
  FROM MATCH (c2:customer)-[:likes]->(p:product),
  (p:product)-[:part_of]->(cat:category)
  WHERE cat.name = 'Beverages'
);

-- Find pairs of brands that share at least one follower (exactly 2 follow-hops).
SELECT DISTINCT b1.name, b2.name
FROM MATCH (b1:brand)-[:follows{2,2}]-(b2:brand)
WHERE b1.id < b2.id;

-- Find any shortest connection (up to 6 hops) between customer 5 and customer 15
-- through shared brand follows. (Customer 1 follows no brand, so it has no path.)
SELECT c1.company_name, c2.company_name
FROM MATCH ANY_SHORTEST (c1:customer)-[f:follows{1,6}]-(c2:customer)
WHERE c1.id = 5
AND c2.id = 15;

------------------------------------------------------------------------
-- Hybrid queries - graph objects combined with relational tables
-- (orders, order_items and countries are relational tables joined to the
-- graph by shared IDs / the country code)
------------------------------------------------------------------------

-- What are the top 10 best-selling products by total quantity sold, with category?
SELECT p.name AS product, c.name AS category, SUM(oi.quantity) AS totalSold
FROM MATCH (p:product)-[:part_of]->(c:category),
  order_items oi
WHERE oi.product_id = p.id
GROUP BY p.name, c.name
ORDER BY totalSold DESC
LIMIT 10;

-- Which product categories generate the most sales revenue?
SELECT c.name AS category,
  SUM(oi.quantity * p.unit_price * (1 - oi.discount_pct / 100.0)) AS revenue
FROM MATCH (p:product)-[:part_of]->(c:category),
  order_items oi
WHERE oi.product_id = p.id
GROUP BY c.name
ORDER BY revenue DESC;

-- Who are our 5 best customers by total amount spent, and what city are they in?
-- (The product vertex table is joined like a relational table on oi.product_id; a
-- separate unconnected (p:product) pattern here makes the query ~100x slower.)
SELECT cu.company_name, ci.name AS city,
  SUM(oi.quantity * p.unit_price * (1 - oi.discount_pct / 100.0)) AS totalSpent
FROM MATCH
  (cu:customer)-[:resides_in]->(ci:city),
  orders o,
  order_items oi,
  product p
WHERE o.customer_id = cu.id
AND oi.order_id = o.id
AND oi.product_id = p.id
GROUP BY cu.id, cu.company_name, ci.name
ORDER BY totalSpent DESC
LIMIT 5;

-- Which products manufactured by "Nike" were ordered, and in what total quantity?
SELECT p.name AS product, SUM(oi.quantity) AS totalQty
FROM MATCH (b:brand)-[:manufactures]->(p:product),
  order_items oi
WHERE b.name = 'Nike'
AND oi.product_id = p.id
GROUP BY p.name
ORDER BY totalQty DESC;

-- Which countries generate the most revenue? (graph cities + countries + order tables)
SELECT co.name AS country,
  SUM(oi.quantity * p.unit_price * (1 - oi.discount_pct / 100.0)) AS revenue
FROM MATCH
  (cu:customer)-[:resides_in]->(ci:city),
  countries co,
  orders o,
  order_items oi,
  product p
WHERE ci.country_code = co.code
AND o.customer_id = cu.id
AND oi.order_id = o.id
AND oi.product_id = p.id
GROUP BY co.name
ORDER BY revenue DESC;

-- List orders placed by customers who follow a brand located in London.
SELECT DISTINCT cu.company_name, o.id AS order_id
FROM MATCH
  (cu:customer)-[:follows]->(b:brand),
  (b:brand)-[:located_at]->(ci:city),
  orders o
WHERE ci.name = 'London'
AND o.customer_id = cu.id;

